package com.int_tech.control.controller;

import com.int_tech.utils.DateTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by liubch on 2018/1/25.
 */
public class TestMain {

    public static void main(String[] arg){

        String partitionLocation = "30.456089_114.05477";
        String s1 = partitionLocation.substring(0,partitionLocation.lastIndexOf("_"));
        String s2 = partitionLocation.substring(partitionLocation.lastIndexOf("_")+1,partitionLocation.length());

        String yjTime = "20180308123101.000";
        //创建时间
        String year = yjTime.substring(0,4);
        String month = yjTime.substring(4,6);
        String day = yjTime.substring(6,8);
        String hour = yjTime.substring(8,10);
        String min = yjTime.substring(10,12);
        String second = yjTime.substring(12,14);

        String dt = year+"-"+month+"-"+day+" "+hour+":"+min+":"+second;

        Date cd = DateTool.string2Date(dt,"yyyy-MM-dd HH:mm:ss");


        Calendar cal = Calendar.getInstance();
        cal.setTime(cd);//date 换成已经已知的Date对象
        cal.add(Calendar.HOUR_OF_DAY, 8);// before 8 hour
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(format.format(cal.getTime()));

        cd = DateTool.string2Date(format.format(cal.getTime()),"yyyy-MM-dd HH:mm:ss");


        String x = "116.5389959913";
        String y = "39.7196130840";

        String url = "http://api.map.baidu.com/geoconv/v1/?coords="+x+","+y+"&from=1&to=5&ak=TsCP2qXsP0RLmchQ7cQYyQa1C5Cwh4er";

        String result = getJson(url);

        JSONObject object = JSONObject.fromObject(result);

        System.out.print(object.toString());

        JSONArray result1 = (JSONArray) object.get("result");

        System.out.print(result1);

        object = result1.getJSONObject(0);

        System.out.print(object.toString());
        x = String.valueOf((Double) object.get("x"));
        y = String.valueOf((Double) object.get("y"));

        System.out.print(x);
        System.out.print(y);

    }


    public static String getJson(String urlstr) {
        org.apache.http.client.HttpClient httpclient = new DefaultHttpClient();
        HttpGet method = new HttpGet(urlstr);
        method.setHeader("Content-type" , "application/json");
        HttpResponse response = null;
        try {
            response = httpclient.execute(method);
            if (response != null) {
                // HttpEntity resEntity = response.getEntity();
                return EntityUtils.toString(response.getEntity());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}
